//
//  MenuTuiJIanCaiNiViewController.m
//  Products_B
//
//  Created by lanou on 16/6/2.
//  Copyright © 2016年 朋学友. All rights reserved.
//


//  methodName=UserLikes&page=1&size=10&version=4.4


#import "MenuTuiJIanCaiNiViewController.h"
#import "MenuFirstModel.h"
#import "MenuFenleiListTableViewCell.h"
#import "MenuFirstDetailViewController.h"
@interface MenuTuiJIanCaiNiViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong)NSMutableDictionary *parDic;
@property (nonatomic, strong)NSString *page;
@property (nonatomic,strong)NSMutableArray *modelArray;
@property (nonatomic,strong)UITableView *tableView;
@end

@implementation MenuTuiJIanCaiNiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.modelArray = [NSMutableArray array];
    self.navigationController.navigationBar.hidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    // Do any additional setup after loading the view.
    self.page = @"1";
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight- 64 - 40) style:(UITableViewStylePlain)];
    self.tableView.delegate =self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = 180;
    [self.tableView registerClass:[MenuFenleiListTableViewCell class] forCellReuseIdentifier:@"MenuFenleiListTableViewCell"];
    
    [self.view addSubview:_tableView];
    
//    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
//        self.page = [NSString stringWithFormat:@"%ld",[self.page integerValue] +1];
//        self.parDic[@"page"] = self.page;
//        [self requestData];
//    }];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
       self.page = @"1";
        self.parDic[@"page"] = self.page;
        [self requestData];
    }];
    
    [self requestData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.modelArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFenleiListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MenuFenleiListTableViewCell" forIndexPath:indexPath];
    MenuFirstModel *model = self.modelArray[indexPath.row];
    cell.model = model;
    return cell;
}
- (void)requestData{
    self.parDic =[@{@"methodName":@"UserLikes",@"page":self.page,@"size":@"10",@"version":@"4.4"}mutableCopy];
    [RequestManager requestWithUrl:@"http://api.izhangchu.com/" requestType:(RequestTypePOST) parDic:self.parDic finish:^(NSData *data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",jsonDic);
        if ([self.page integerValue] == 1) {
            [self.modelArray removeAllObjects];
        }
        NSArray *array = [MenuFirstModel modelConfigureWithDic:jsonDic];
        for (MenuFirstModel *model in array) {
            [self.modelArray addObject:model];
        }
        NSLog(@"%@",self.modelArray);
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self.tableView reloadData];
    } error:nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MenuFirstDetailViewController *fiVc = [[MenuFirstDetailViewController alloc]init];
    MenuFirstModel *model = self.modelArray[indexPath.row];
    fiVc.dishes_id = model.dishes_id;
    [self.navigationController pushViewController:fiVc animated:YES];
}
#pragma mark - 当push的时候隐藏tabBar
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillDisappear:(BOOL)animated{
    self.navigationController.navigationBar.hidden = YES;
}
//- (void)viewWillDisappear:(BOOL)animated{
//    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.hidden = YES;
//}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
